package cn.hotel.dao;

import cn.hotel.pojo.HouseType;
import cn.hotel.utlis.Crud;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class RoomKeeperDao extends Crud implements RoomKeeper {
    @Override
    public List<cn.hotel.pojo.RoomKeeper> rkl(int room, int atPresent, int sum) {
        String sql = "SELECT R.roomNumber,R.condition,H.houseType,H.price FROM `ROOM_KEEPER` R JOIN house_type H ON R.houseTypeId = H.id ";
        StringBuffer sbf = new StringBuffer(sql);
        List<Object> data = new ArrayList<>();
        if(room > 0){
            sbf.append(" WHERE R.roomNumber = ? ");
            data.add(room);
        }
        sbf.append(" LIMIT ?,?");
        data.add(atPresent);
        data.add(sum);
        ResultSet resultSet = getResultSet(sbf.toString(),data);
        List<cn.hotel.pojo.RoomKeeper> roomKeepers = new ArrayList<>();
        cn.hotel.pojo.RoomKeeper roomKeeper = null;
        HouseType houseType = null;
        try {
            while (resultSet.next()){
                roomKeeper = new cn.hotel.pojo.RoomKeeper();
                roomKeeper.setRoomNumber(resultSet.getInt(1));
                roomKeeper.setCondition(resultSet.getInt(2));
                houseType = new HouseType();
                houseType.setHouseType(resultSet.getString(3));
                houseType.setPrice(resultSet.getString(4));
                roomKeeper.setHouseType(houseType);
                roomKeepers.add(roomKeeper);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            this.setClear(resultSet);
        }
        return roomKeepers;
    }

    @Override
    public int roomKeeper(int room) {
        String sql = "select COUNT(*) FROM ROOM_KEEPER ";
        StringBuffer sbf = new StringBuffer(sql);
        List<Object> data = new ArrayList<>();
        if(room > 0){
            sbf.append(" WHERE roomNumber = ?");
            data.add(room);
        }
        ResultSet resultSet = getResultSet(sbf.toString(),data);
        int num = 0;
        try {
            while (resultSet.next()){
                num = resultSet.getInt(1);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            this.setClear(resultSet);
        }
        return num;
    }

    @Override
    public int addRoomKeeper(cn.hotel.pojo.RoomKeeper roomKeeper) {
        String sql = "INSERT INTO `HOTEL`.`ROOM_KEEPER` (`ROOMNUMBER`, `HOUSETYPEID`, `CONDITION`) VALUES (?, ?, 0)";
        List<Object> data = new ArrayList<>();
        data.add(roomKeeper.getRoomNumber());
        data.add(roomKeeper.getHouseTypeId());
        return getInt(sql,data);
    }

    @Override
    public int deleteRoomKeeper(int room) {
        String sql = "DELETE FROM ROOM_KEEPER WHERE ROOMNUMBER = ?";
        List<Object> data = new ArrayList<>();
        data.add(room);
        return getInt(sql,data);
    }

    @Override
    public int modification(cn.hotel.pojo.RoomKeeper roomKeeper) {
        return 0;
    }

    @Override
    public int numType(int num) {
        String sql = "SELECT COUNT(1) FROM ROOM_KEEPER WHERE ROOMNUMBER=? ";
        List<Object> data = new ArrayList<>();
        data.add(num);
        int nums = 0;
        ResultSet resultSet = getResultSet(sql,data);
        try {
            while (resultSet.next()){
                nums = resultSet.getInt(1);
            }
            return nums;
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            this.setClear(resultSet);
        }
        return nums;
    }
}
